clear;
close all;
clc;

%============Change the following parameters
Sub_Pro = 2; % 3.2
% Sub_Pro = 3; % 3.3
for I_trial = 1:10 % change I_trial from 1 to 10, which will correspond to the values below.

    if Sub_Pro==2
        precuts  =[ 2.7  2.8  3.1  2.4  3.0  2.7  2.6  2.7  3.3  2.8].*41./44.1;% ********* Protocol 3.2 this is good for 3.1
    elseif Sub_Pro==3
        precuts  =[ 4.2  2.7  2.5  2.6  2.7  3.1  2.7  2.7  3  2.6].*41./44.1;% ********* Protocol 3.3 this is good for 3.1
    end

    %============Do not change the following parameters
    Proto_Num = 3;
    % fs = 41000;
    fs = 44100;
    conds = 'AB';
    sides = {'Left'}; % fake for Protocol = 3


    [b a]=butter(3,1/fs,'high');
    precut=precuts(I_trial)
    eval(['load All_sound_recordings\Phonemes\Phoneme_' num2str(Proto_Num) '.' num2str(Sub_Pro) '\Phoneme_' num2str(Proto_Num) '.' num2str(Sub_Pro) '_' sides{1} '_'   num2str((I_trial)) 'degree.mat']);


    load 'C:\Users\gaiy\OneDrive - Saint Louis University\speech_experiments\ori';

    % % % % % % % [Y Ind]=findpeaks(sound_left,32000,'MinPeakHeight',0.15)
    % % % % % % % true_dur = diff(Ind)
    % % % % % % % save record_durs true_dur
    load record_durs
    % dur = [true_dur true_dur(end)]+0.14
    dur = [true_dur true_dur(end)]

    % sound([sound_left' 0.*sound_left'],Fs,16)
    % recorded_sound=resample(sound_left.*10,fs,Fs);
    % sound([recorded_sound' 0.*recorded_sound'],fs,16)


    left_sound = filter(b,a,recorded_sound(:,1));
    right_sound = filter(b,a,recorded_sound(:,2));
    % left_sound = filter(b,a,recorded_sound');
    % right_sound = filter(b,a,recorded_sound');

    % sound(left_sound,fs);
    % pause

    left_sound=left_sound(round(precut.*fs):end);
    right_sound=right_sound(round(precut.*fs):end);

    figure;
    plot(left_sound,'b')
    hold on;
    plot(right_sound,'r')
    % sdf9
    % sound(recorded_sound,fs);
    % pause(length(recorded_sound)./fs.*0.8)

    % cutting:
    for ip = 1:24
        ip
        time_sofar= sum(dur(1:ip-1));
        durN = round(dur(ip).*fs);
        startP=round(time_sofar*fs);
        pcut = [left_sound(startP+1:min([end startP+durN])) right_sound(startP+1:min([end startP+durN]))];
        wholeramp=f_getRamp(0.05,length(pcut)./fs,fs);
        pcut =pcut .*wholeramp';
        area([startP+1 startP+durN],[0.4 0.4],0.2)
        title(num2str(ip))
        % sound(pcut,fs);
        % pause(dur(ip)+1)
        eval(['save cut_Ps_corrected\Phoneme' num2str(ip) '_P' num2str(Sub_Pro) '_v' num2str(I_trial) ' pcut fs']);

        % pause9
    end

end